package com.leetcode.sorts.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: WuPeng
 * @Date: 2021/6/3 3:13 下午
 *
 * 429.N叉树的层序遍历
 *
 *
 */
public class LevelOrderOfNTree_429 {
    class Node {
        public int val;
        public List<Node> children;
        public Node() {}
        public Node(int _val) {
            val = _val;
        }
        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    public static List<List<Integer>> levelOrder(Node node){
        List<List<Integer>> res = new ArrayList<>();
        if (node == null){
            return res;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(node);
        while (!queue.isEmpty()){
            List<Integer> level_list = new ArrayList<>();
            int level_size = queue.size();
            for (int i = 0; i < level_size; i++) {
                Node node_tmp = queue.poll();
                level_list.add(node_tmp.val);
                if (node_tmp.children != null){
                    queue.addAll(node_tmp.children);
                }
            }
            res.add(level_list);
        }
        return res;
    }

}
